/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */
package com.credithc.eagleeye.redis;
import com.credithc.eagleeye.monitor.EagleMonitorMgr;
import com.credithc.framework.redis.util.RedisConfiguration;
import com.credithc.framework.redis.util.RedisSingleUtil;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Protocol;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2016年09月26日<br/>
 * 版 本：v1.0.0<br>
 * <p/>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
public class EagleRedisMgr {
    protected static final String redisConfigFilePath = "property/eagle-monitor-config.properties";
    private volatile static EagleRedisMgr instance;
    private final Logger logger = LoggerFactory.getLogger(EagleMonitorMgr.class);
    private Thread redisPopThread;
    private EagleRedisMgr() {
    }
    public static EagleRedisMgr getInstance() {
        if (instance == null) {
            synchronized (EagleRedisMgr.class) {
                if (instance == null) {
                    instance = new EagleRedisMgr();
                }
            }
        }
        return instance;
    }
    public void startRedisPopThread() {
        if (redisPopThread == null) {
            try {
                Properties localProperties = loadLocalProperties();
                RedisConfiguration redisConfiguration = loadRedisConfiguration(localProperties);
                RedisSingleUtil redisSingleUtil = new RedisSingleUtil(redisConfiguration);
                redisPopThread = new Thread(new EagleRedisQueueTask(redisSingleUtil));
                redisPopThread.setDaemon(true);
                redisPopThread.start();
                logger.info(String.format("eagle-eye redis pop thread is start!redis host:%s", redisConfiguration.getAddress()));
            } catch (Exception ex) {
                logger.error(String.format("eagle-eye redis pop thread is error! exception:%s", ex.getMessage()));
            }
        }
    }
    public void stopRedisPopThread() {
        if (redisPopThread != null) {
            redisPopThread.interrupt();
            logger.info("eagle-eye redis pop thread is stop!");
        }
    }
    public Properties loadLocalProperties() {
        Properties localProperties = new Properties();
        try (InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(redisConfigFilePath)) {
            if (null == input) {
                throw new RuntimeException(redisConfigFilePath);
            }
            localProperties.load(input);
            return localProperties;
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
    public RedisConfiguration loadRedisConfiguration(Properties localProperties) {
        if (localProperties == null) {
            return null;
        }
        boolean enable = Boolean.parseBoolean(localProperties.getProperty("eagle_monitor_redis_enable", "true"));
        if (!enable) {
            throw new RuntimeException("eagle_monitor_redis_enable is false!");
        }
        String address = localProperties.getProperty("eagle_monitor_redis_address", "");
        if (org.apache.commons.lang.StringUtils.isBlank(address)) {
            throw new RuntimeException("error:redis adress is blank!");
        }
        //鹰眼目前只用单机版redis
        int mode = Integer.parseInt(localProperties.getProperty("mode", "1"));
        int maxTotal = Integer.parseInt(localProperties.getProperty("eagle_monitor_redis_maxTotal", GenericObjectPoolConfig.DEFAULT_MAX_TOTAL + ""));
        int minIdle = Integer.parseInt(localProperties.getProperty("eagle_monitor_redis_minIdle", GenericObjectPoolConfig.DEFAULT_MIN_IDLE + ""));
        int maxIdle = Integer.parseInt(localProperties.getProperty("eagle_monitor_redis_maxIdle", GenericObjectPoolConfig.DEFAULT_MAX_IDLE + ""));
        long maxWaitMillis = Integer.parseInt(localProperties.getProperty("eagle_monitor_redis_maxWaitMillis", GenericObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS + ""));
        int timeout = Integer.parseInt(localProperties.getProperty("eagle_monitor_redis_timeout", Protocol.DEFAULT_TIMEOUT + ""));
        RedisConfiguration redisConfiguration = new RedisConfiguration();
        redisConfiguration.setAddress(address);
        redisConfiguration.setMode(1);
        redisConfiguration.setMaxRedirections(6);
        redisConfiguration.setDatabase(0);
        redisConfiguration.setMaxIdle(maxIdle);
        redisConfiguration.setMinIdle(minIdle);
        redisConfiguration.setMaxWaitMillis(maxWaitMillis);
        redisConfiguration.setTimeout(timeout);
        return redisConfiguration;
    }
}
